package com.yuerlive.rainbow;

import android.content.Context;
import android.os.Build;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.squareup.leakcanary.LeakCanary;
import com.tencent.bugly.crashreport.CrashReport;
import com.umeng.analytics.MobclickAgent;
import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.yuerlive.rainbow.common.AppConfig;
import com.yuerlive.rainbow.common.Constants;
import com.yuerlive.rainbow.entity.User;
import com.yuerlive.rainbow.entity.UserInfo;
import com.yuerlive.rainbow.nim.NimHelper;
import com.yuerlive.rainbow.utils.PreferenceUtils;
import com.yuerlive.rainbow.utils.ProcessUtil;
import com.yuerlive.rainbow.utils.UserProxy;
import com.yuerlive.rainbow.utils.Utils;
import com.yuerlive.rainbow.widget.timeselector.TextUtil;

import java.util.Date;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by xiaoyi on 2017/5/17.
 */

public class RainbowApp extends MultiDexApplication {
    private static RainbowApp app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        Config.DEBUG = false;
        UMShareAPI.get(this);
        PlatformConfig.setWeixin(Constants.WEIXIN_APP_ID, Constants.WEIXIN_APP_SECRET);
        PlatformConfig.setSinaWeibo(Constants.WEIBO_APP_KEY, Constants.WEIBO_APP_SECRET, "http://open.weibo.com/apps/448599533/privilege/oauth");
        PlatformConfig.setQQZone(Constants.QQ_APP_ID, Constants.QQ_APP_KEY);
        MobclickAgent.openActivityDurationTrack(false);
        init();
//        Utils.checkAPP(app);
        //解决7.0系统拍照FileUriExposedException问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
            builder.detectFileUriExposure();
        }
    }

    public static RainbowApp getContext() {
        return app;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void init() {
        NimHelper.init(this);
        if (ProcessUtil.inMainProcess(this)) {
            initLeakCanary();
            initBugly();
            initStartTime();

            /** JPush相关 **/
            Log.e("APPPP", "JPush before");
            JPushInterface.setDebugMode(false); // 设置开启日志,发布时请关闭日志
            JPushInterface.init(this);
            Log.e("APPPP", "JPush end");
        }

        initHost();
    }

    private void initHost() {
        if (BuildConfig.DEBUG) {
            String host = PreferenceUtils.getString("host");
            if (!TextUtil.isEmpty(host)) {
                API.HOST = host;
            }
        }
    }

    private void initLeakCanary() {
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
    }

    private void initBugly() {
        if (AppConfig.isRelease()) {
            CrashReport.initCrashReport(this, Constants.BUGLY_ID, true);
        }

    }

    private void initStartTime() {
        if (UserProxy.getUser() == null) return;
        long current = System.currentTimeMillis();
        String prefix = UserProxy.getUser().getUserId();
        if (PreferenceUtils.getLong(prefix + "_START_TIME") <= 0) {
            PreferenceUtils.putLong(prefix + "_START_TIME", current);
        } else {
            Date date = new Date(PreferenceUtils.getLong(prefix + "_START_TIME"));
            Date currentDate = new Date(current);
            if (date.getDate() == currentDate.getDate() && date.getMonth() == currentDate.getMonth()) { //同一天
                //PreferenceUtils.putLong("START_TIME", current);
            } else {
                PreferenceUtils.putLong(prefix + "_START_TIME", current);  //新的一天
                UserInfo info = UserProxy.getUser().getUserInfo();
                info.setGetSweetsCount(0);
                User user = UserProxy.getUser();
                user.setUserInfo(info);
                UserProxy.setUser(user);
            }
        }
    }
}
